import { baseType } from '../interface/shapes'
import { _renderPaintInOrder } from '../util/style_class'

export default class Base {
  x = 0
  y = 0
  rotate = 0
  width = 0
  height = 0
  fill = ''
  alpha = ''
  stroke = ''
  strokeWidth = ''
  shadow = ''
  constructor(data: baseType) {
    for (let key in data) {
      ;(this as any)[key] = (data as any)[key]
    }
  }
  async _renderStyle(ctx: any) {
    ctx.setTransform(1, 0, 0, 1, 0, 0)
    ctx.rotate((this.rotate * Math.PI) / 180)
    if (this.shadow) {
      this._renderShadow(ctx)
    }
    this._renderLine(ctx)
    await _renderPaintInOrder(ctx, this.fill, this.stroke)
  }
  _renderShadow(ctx: any) {
    let shadow = this.shadow.split(' ')
    ctx.shadowOffsetX = Number(shadow[0])
    ctx.shadowOffsetY = Number(shadow[1])
    ctx.shadowBlur = Number(shadow[2])
    ctx.shadowColor = shadow[3]
  }
  _renderLine(ctx: any) {
    ctx.setLineDash([])
    ctx.lineDashOffset = -90
    ctx.lineWidth = this.strokeWidth
    ctx.lineCap = 'round'
  }
}
